
from fastapi import APIRouter, Form

from dto import ApiResponse
from service import user_serivce

user_router = APIRouter(
    prefix="/user",
    tags=["用户相关:登录/注册/登出等"],
)


@user_router.post("/register")
async def test(username: str = Form(...),
               password: str = Form(...),
               email: str = Form(...)):

    user = await user_serivce.register_user(username, password, email)

    return ApiResponse.success({
        "username": user.username,
        "email": user.email
    })


# post encode url参数：
@user_router.post("/login", summary="用户登录")
async def login(username: str = Form(...), password: str = Form(...)):

    print(f"user = {username}, pwd = {password}")
    try:
        return await user_serivce.login(username, password)
    except Exception as e:
        return ApiResponse.fail(
            message=str(e)
        )


# 路径参数
@user_router.get("/info/{user_id}")
async def user_info(user_id: int):
    return ApiResponse.success(
        data={
            "id": user_id,
            "name": "张三",
            "age": 18
        }
    )


# 查询参数
@user_router.get("/info")
async def user_info(user_id: int):
    return ApiResponse.success(
        data={
            "id": user_id,
            "name": "张三",
            "age": 18
        }
    )

